package liangtWorkSpace.springTest;

import liangtWorkSpace.springTest.dao.PersonDao;
import liangtWorkSpace.springTest.dao.PersonService;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @author liangt
 * @ClassName: MyTest
 * @title: MyTest
 * @projectName liangtWorkSpace.springTest
 * @description: TODO
 * @date 2020/10/10 18:51
 */
public class MyTest {

    public static void main(String[] args) {
        //定义配置文件路径
        String xml = "applicationContextSpring.xml";
        //初始化Spring容器，加载配置文件
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xml);
        PersonDao personDap = (PersonDao) applicationContext.getBean("personDao");
        personDap.add();
    }

    @Test
    public void test2(){
        // 定义Spring配置文件的路径
        String xmlPath = "applicationContextSpring.xml";
        // 初始化Spring容器，加载配置文件
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
                xmlPath);
        // 通过容器获取personService实例
        PersonService personService = (PersonService) applicationContext
                .getBean("personService");
        // 调用personService的addPerson()方法
        personService.addPerson();
    }

    /**
     * 构造参数实例化演示
     * 上述文件中，首先在 test() 方法中定义了 Spring 配置文件的路径，然后 Spring 容器会加载配置文件。在加载的同时，Spring 容器会通过实现类 Person1 中默认的无参构造函数对 Bean 进行实例化
     */
    @Test
    public void test3(){
        //定义Spring 的xml实例化配置文件地址
        String xmlPath = "applicationContextSpring.xml";
        //初始化Application
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
        System.out.println(applicationContext.getBean("person1"));
    }

    /**
     * 静态工厂实例化
     * 义了一个 id 为 person2 的 Bean，其中 class 属性指定了其对应的工厂实现类为 MyBeanFactory，而 factory-method 属性用于告诉 Spring 容器调用工厂类中的 createBean() 方法获取 Bean 的实例
     */
    @Test
    public void test4(){
        String xmlPath = "applicationContextSpring.xml";
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
        System.out.println(applicationContext.getBean("person2"));
    }

    /**
     * 实例化工厂
     */
    @Test
    public void test5(){
        String xmlPath = "applicationContextSpring.xml";
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
        System.out.println(applicationContext.getBean("person3"));
    }

    /**
     * Spring 作用域--单例
     */
    @Test
    public void test6(){
        String xmlPath = "applicationContextSpring.xml";
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);

        System.out.println(applicationContext.getBean("personSingle"));
        System.out.println(applicationContext.getBean("personSingle"));

    }

    /**
     * Spring 作用域--原型
     */
    @Test
    public void test7(){
        String xmlPath = "applicationContextSpring.xml";
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);

        System.out.println(applicationContext.getBean("personPrototype"));
        System.out.println(applicationContext.getBean("personPrototype"));
    }

}
